package com.taxi.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.taxi.bean.ChoferBean;
import com.taxi.bean.EmpresaBean;
import com.taxi.bean.VehiculoBean;
import com.taxi.service.ChoferService;
import com.taxi.service.EmpresaService;
import com.taxi.service.GestionUsuarioBusinessDelegate;
import com.taxi.service.PaqueteBusinessDelegate;
import com.taxi.service.VehiculoService;


public class ChoferAction extends ActionSupport implements ServletRequestAware  {
	
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private ChoferService choferservice = PaqueteBusinessDelegate.getChoferService();
	private EmpresaService empresaservice = PaqueteBusinessDelegate.getEmpresaService();
	private VehiculoService vehiculoservice = PaqueteBusinessDelegate.getVehiculoService();
	private ChoferBean chofer;
	private EmpresaBean empresa;
	private VehiculoBean vehiculoBean;
	private List<ChoferBean> choferes;
	private List<EmpresaBean> empresas;
	private List<VehiculoBean> vehiculos;
	
	private boolean success;
	private String msg;
	
	 
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public String registrarChofer() throws Exception {
		
		chofer = new ChoferBean();
		
		chofer.setNombres(this.request.getParameter("nombres"));
		chofer.setApellidos(this.request.getParameter("apellidos"));
		chofer.setTelefono(Integer.parseInt(this.request.getParameter("telefono")));
		chofer.setNextel(Integer.parseInt(this.request.getParameter("nextel")));
		chofer.setTipoMovil(this.request.getParameter("tipoMovil"));
		chofer.setNroMovil(Integer.parseInt(this.request.getParameter("movil")));
		
		vehiculoBean = new VehiculoBean();
		vehiculoBean.setCodigo(this.request.getParameter("vehiculo"));
	
		System.out.println(this.request.getParameter("vehiculo"));
		System.out.println(vehiculoBean.getNumeroplaca());
		
		chofer.setVehiculo(vehiculoBean);
				
		try {
				choferservice.registrarchofer(chofer);
				setSuccess(true);		
				setMsg("Chofer registrado con exito");
				
		} catch (Exception e) {
			setSuccess(false);
			System.out.println(e.getStackTrace());
			setMsg("Error al registrar nuevo chofer");
		}

		return SUCCESS;
	}
	
	public String actualizarChofer() throws Exception {
		
		chofer = new ChoferBean();
		
		chofer.setCodigo(Integer.parseInt(this.request.getParameter("codigo")));		
		chofer.setNombres(this.request.getParameter("nombres"));
		chofer.setApellidos(this.request.getParameter("apellidos"));
		chofer.setTelefono(Integer.parseInt(this.request.getParameter("telefono")));
		chofer.setNextel(Integer.parseInt(this.request.getParameter("nextel")));
		chofer.setTipoMovil(this.request.getParameter("tipoMovil"));
		chofer.setNroMovil(Integer.parseInt(this.request.getParameter("nroMovil")));
		
		vehiculoBean = new VehiculoBean();
		
		vehiculoBean.setCodigo(this.request.getParameter("vehiculo.codigo"));
	
		System.out.println(this.request.getParameter("vehiculo.codigo"));
		
		chofer.setVehiculo(vehiculoBean);
				
		try {
				choferservice.actualizarChofer(chofer);
				setSuccess(true);		
				setMsg("Chofer actualizado con exito");
				
		} catch (Exception e) {
			setSuccess(false);
			System.out.println(e.getStackTrace());
			setMsg("Error al actualizar datos de chofer");
		}
		
		return SUCCESS;
	}
	
	public String eliminarChofer() throws Exception {

		System.out.println("ENTRO AL eliminar");
		
		chofer= new ChoferBean();
		String codigo = this.request.getParameter("codigo");
		
		System.out.println(codigo);
		
        //eliminar empresa

		try {
			choferservice.eliminarChofer(codigo);			
			setSuccess(true);
			setMsg("Chofer elimando con exito");
		} catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
			setMsg("Error al eliminar chofer");
		}
		 return SUCCESS;
	}
	
	public String listaChofer() throws Exception{
		
		try{
			choferes = choferservice.listaChofer();
			setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
		}
		//VehiculoBean bean = (VehiculoBean) listaVehiculo.get(0);
		
		return SUCCESS;
	}

	public String listarPlacas() throws Exception{
		
		vehiculos = vehiculoservice.listaVehiculo();
		
		return SUCCESS;
	}
	public String listarxnombresChofer() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String nombres = request.getParameter("nombres");
		chofer = choferservice.listarxnombresChofer(nombres);
		return SUCCESS;
	}	
	
	
	public String listarChofer() throws Exception{
		return SUCCESS;
	}
	
	public String listarEmpresas() throws Exception{
		
		empresas = empresaservice.listaEmpresa();
		//this.listarPlacas();
		
		return SUCCESS;
	}
	
	public String obtenerPlacasPorEmpresa() throws Exception{
		
		System.out.println(this.request.getParameter("varGS"));
				
		int codigo = Integer.parseInt(this.request.getParameter("varGS"));
		
		try{
			vehiculos = vehiculoservice.listarVehiculosPorEmpresa(codigo);
			setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			setSuccess(false);	
		}
		
		// this.listarEmpresas();
		setSuccess(true);
		return SUCCESS;
	}	
	
	
	
	
	public EmpresaBean getEmpresa() {
		return empresa;
	}

/*
	public void setEmpresa(EmpresaBean empresa) {
		this.empresa = empresa;
	}
*/

	public List<EmpresaBean> getEmpresas() {
		return empresas;
	}


	public void setEmpresas(List<EmpresaBean> empresas) {
		this.empresas = empresas;
	}


	public ChoferBean getChofer() {
		return chofer;
	}

	public void setChofer(ChoferBean chofer) {
		this.chofer = chofer;
	}


	public List<ChoferBean> getChoferes() {
		return choferes;
	}


	public void setChoferes(List<ChoferBean> choferes) {
		this.choferes = choferes;
	}


	public List<VehiculoBean> getVehiculos() {
		return vehiculos;
	}


	public void setVehiculos(List<VehiculoBean> vehiculos) {
		this.vehiculos = vehiculos;
	}


	public VehiculoBean getVehiculo() {
		return vehiculoBean;
	}

/*
	public void setVehiculo(VehiculoBean vehiculoBean) {
		this.vehiculoBean = vehiculoBean;
	}
	
*/
	public boolean isSuccess() {
		return success;
	}
	
	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	

	

	
}
